package com.apiit.webserviceclient.activities;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import com.apiit.webserviceclient.R;
import com.apiit.webserviceclient.R.layout;
import com.apiit.webserviceclient.R.menu;
import com.apiit.webserviceclient.utility.BPEL_Process;
import com.apiit.webserviceclient.utility.BPEL_Request;
import com.apiit.webserviceclient.utility.BPEL_Response;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class LecturerPagol extends Activity implements OnClickListener {

	Button currentStudents;
	Button viewProfile;
	Button viewppfs;
	Button viewpsfs;
	Button midpointlist;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lecturer_pagol);

		currentStudents = (Button) findViewById(R.id.btncurrentstudents);
		viewProfile = (Button) findViewById(R.id.btnpagolprofile);
		viewpsfs = (Button) findViewById(R.id.btnpsf);
		viewppfs = (Button) findViewById(R.id.btnmidpoint);

		currentStudents.setOnClickListener(this);
		viewProfile.setOnClickListener(this);
		viewppfs.setOnClickListener(this);
		viewpsfs.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.lecturer_pagol, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == viewProfile) {
			// http://webspace1.apiit.lk/pagol15/www/student_pp.asp
			// Get

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/lecturer_pp.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("lecturerID",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[2]/td[3]---");
			xpathMap.put("name",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[3]/td[3]---");
			xpathMap.put("email",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[4]/td[3]---");
			xpathMap.put("contactNo",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[5]/td[3]---");
			xpathMap.put(
					"skill1",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[6]/td[3]/font[1]---");
			xpathMap.put(
					"skill2",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[7]/td[3]/font[1]---");
			xpathMap.put(
					"skill3",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[8]/td[3]/font[1]---");
			xpathMap.put(
					"skill4",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[9]/td[3]/font[1]---");
			xpathMap.put(
					"skill5",
					"/html[1]/body[1]/center[1]/table[1]/tr[1]/td[1]/table[1]/tr[10]/td[3]/font[1]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");

			BPEL_Response respones = null;

			try {
				respones = bpelProcess.doRequest(request);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String lecturerID = respones.getResultsMap().get("lecturerID")
					.trim();
			String name = respones.getResultsMap().get("name").trim();
			String email = respones.getResultsMap().get("email").trim();
			String contactno = respones.getResultsMap().get("contactNo").trim();
			String skill1 = respones.getResultsMap().get("skill1").trim();
			String skill2 = respones.getResultsMap().get("skill2").trim();
			String skill3 = respones.getResultsMap().get("skill3").trim();
			String skill4 = respones.getResultsMap().get("skill4").trim();
			String skill5 = respones.getResultsMap().get("skill5").trim();

			Toast.makeText(getApplicationContext(), lecturerID,
					Toast.LENGTH_LONG).show();

			Intent i = new Intent(this, LecturerProfile.class);
			i.putExtra("lecturerID", lecturerID);
			i.putExtra("name", name);
			i.putExtra("email", email);
			i.putExtra("contactNo", contactno);
			i.putExtra("skill1", skill1);
			i.putExtra("skill2", skill2);
			i.putExtra("skill3", skill3);
			i.putExtra("skill4", skill4);
			i.putExtra("skill5", skill5);
			startActivity(i);
		}
		if (v == viewppfs) {
			Intent i = new Intent(this, ViewStudentPPF.class);
			startActivity(i);
		}
		if (v == viewpsfs) {
			Intent i = new Intent(this, ViewStudentPSF.class);
			startActivity(i);
		}
		if (v == currentStudents) {

			BPEL_Process bpelProcess = new BPEL_Process();
			BPEL_Request request = new BPEL_Request();
			request.setUrl("http://webspace1.apiit.lk/pagol15/www/assessor_currentstu.asp");
			Map<String, String> xpathMap = new HashMap<String, String>();
			xpathMap.put("message", "/html[1]/body[1]/center[1]/table[1]---");
			request.setXpathMap(xpathMap);
			Map<String, String> parametersMap = new HashMap<String, String>();
			request.setParametersMap(parametersMap);
			request.setType("GET");

			BPEL_Response response = null;

			try {
				response = bpelProcess.doRequest(request);

			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String value = response.getResultsMap().get("message");
			value = value.trim().replaceAll("StudentID", "").replaceAll("Name", "").replaceAll("Supervisor", "").replaceAll("Project Title", "").replaceAll("PPF", "").replaceAll("PSF", "").replaceAll("MID", "").replaceAll("1_6", "").replaceAll("1_7", "").replaceAll("Last Seen", "").replaceAll("Submission", "").replaceAll("&nbsp;", "");
			value = value.trim();
			String[] a = value.split("\n");
			//FileReader fr;
			String [] temparr;
			String tempStringWithSplitter = "";
			BufferedReader br = new BufferedReader(new StringReader(value));
			
				String line;
				try {
					line = br.readLine();
					while (line != null) {
						line = line.trim();
						if(!line.equalsIgnoreCase(""))
						{
							tempStringWithSplitter = tempStringWithSplitter + "====" + line;
						}
						line = br.readLine();
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			temparr = tempStringWithSplitter.split("====");
			Toast.makeText(getApplicationContext(),  value, Toast.LENGTH_LONG)
					.show();

			Intent i = new Intent(this, ViewCurrentlyAssignedStudents.class);
			i.putExtra("value", temparr);
			startActivity(i);
		}

	}
}
